import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Account from '../components/Account/AccountList'
import Authority from '../components/Authority/Authority'
import Role from '../components/Authority/Role'
import GameList from '@/components/Game/GameList'
import TypeList from '@/components/Game/TypeList'
import DeveloperGameList from '@/components/Developer/DeveloperGameList'
import DeveloperDocument from '@/components/Developer/DeveloperDocument'
import GuideList from '@/components/Document/GuideList'
import DeveloperHome from '@/components/Developer/DeveloperHome'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/accounts',
      component: Account
    }, {
      path: '/roles',
      component: Role
    }, {
      path: '/authority',
      component: Authority
    }, {
      path: '/games',
      component: GameList
    }, {
      path: '/gameTypes',
      component: TypeList
    }, {
      path: '/guides',
      component: GuideList
    }]
  },
  {
    path: '/developer',
    component: DeveloperHome,
    children: [
      {
        path: '/developer/gameList',
        component: DeveloperGameList
      }, {
        path: '/developer/document',
        component: DeveloperDocument
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const tokenStr = window.sessionStorage.getItem('Authorization')
  if (!tokenStr) {
    return next('/login')
  } else {
    return next()
  }
})

export default router
